package com.ruoyi.bantong.service.impl;

import java.math.BigDecimal;
import java.util.*;

import com.ruoyi.bantong.domain.*;
import com.ruoyi.bantong.mapper.*;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bantong.service.IBtRedbagPollService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 红包玩法池Service业务层处理
 * 
 * @author laohei
 * @date 2021-02-06
 */
@Service
public class BtRedbagPollServiceImpl implements IBtRedbagPollService 
{
    @Autowired
    private BtRedbagPollMapper btRedbagPollMapper;
    @Autowired
    private BtUserTicketMapper ticketMapper;
    @Autowired
    private BtRedbagPollDetailsMapper pollDetailsMapper;

    @Autowired
    private BtRedbagConfigMapper configMapper;
    @Autowired
    private BtPlacecardApplyMapper applyMapper;

    /**
     * 查询红包玩法池
     * 
     * @param rpId 红包玩法池ID
     * @return 红包玩法池
     */
    @Override
    public BtRedbagPoll selectBtRedbagPollById(Integer rpId)
    {
        return btRedbagPollMapper.selectBtRedbagPollById(rpId);
    }

    /**
     * 查询红包玩法池列表
     * 
     * @param btRedbagPoll 红包玩法池
     * @return 红包玩法池
     */
    @Override
    public List<BtRedbagPoll> selectBtRedbagPollList(BtRedbagPoll btRedbagPoll)
    {
        List<BtRedbagPoll> btRedbagPolls = btRedbagPollMapper.selectBtRedbagPollList(btRedbagPoll);
        if(btRedbagPolls != null && btRedbagPolls.size() > 0){
            BtRedbagPoll poll = btRedbagPolls.get(0);
            Integer status = poll.getStatus();
            Integer type = poll.getType();
            //统计实时业绩
            if(status < 2){ //开奖之前
                if(type == 1){ //日
                    BtRedbagConfig btRedbagConfig = configMapper.selectBtRedbagConfigById(type);
                    Integer openHour = btRedbagConfig.getOpenHour();
                    Calendar cal  = Calendar.getInstance();
                    cal.setTime(new Date());
                    if(cal.get(Calendar.HOUR_OF_DAY) < openHour){
                        cal.add(Calendar.DATE,-1);
                    }
                    cal.set(Calendar.HOUR_OF_DAY, openHour);
                    cal.set(Calendar.MINUTE, 0);
                    cal.set(Calendar.SECOND, 0);
                    Integer percentageSum = applyMapper.selectBtPlacecardApplySumWeek(cal.getTime());
                    btRedbagPolls.get(0).setPercentageFee(new BigDecimal(percentageSum));
                }else{
                    Calendar cal2  = Calendar.getInstance();
                    cal2.setFirstDayOfWeek(Calendar.MONDAY);
                    cal2.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
                    cal2.set(Calendar.HOUR_OF_DAY, 0);
                    cal2.set(Calendar.MINUTE, 0);
                    cal2.set(Calendar.SECOND, 0);
                    Integer percentageSum2 = applyMapper.selectBtPlacecardApplySumWeek(cal2.getTime());
                    btRedbagPolls.get(0).setPercentageFee(new BigDecimal(percentageSum2));
                }
            }
        }
        return btRedbagPolls;
    }

    /**
     * 新增红包玩法池
     * 
     * @param btRedbagPoll 红包玩法池
     * @return 结果
     */
    @Override
    public int insertBtRedbagPoll(BtRedbagPoll btRedbagPoll)
    {
        btRedbagPoll.setCreateTime(DateUtils.getNowDate());
        return btRedbagPollMapper.insertBtRedbagPoll(btRedbagPoll);
    }

    /**
     * 加入红包玩法池
     *
     * @return 结果
     */
    @Override
    @Transactional
    public int addBtRedbagPoll() {
        Integer btUserId = SecurityUtils.getBtUserId();
        List<BtUserTicket> btUserTickets = ticketMapper.selectBtUserTicketListByUserId(btUserId);
        if(StringUtils.isNull(btUserTickets) || btUserTickets.size() == 0){
            throw new CustomException("您没有可参加抽奖的奖券");
        }
        //查询可以加入的奖池
        BtRedbagPoll btRedbagPoll = btRedbagPollMapper.selectBtRedbagPollByStatus(0);
        for (BtUserTicket t : btUserTickets) {
            BtRedbagPollDetails details = new BtRedbagPollDetails();
            details.setRpId(btRedbagPoll.getRpId());
            details.setUserId(btUserId);
            details.setUtId(t.getUtId());
            details.setCreateTime(DateUtils.getNowDate());
            pollDetailsMapper.insertBtRedbagPollDetails(details);
        }
        return 1;
    }

    /**
     * 红包玩法池  状态
     *
     * @return 结果
     */
    @Override
    @Transactional
    public Map<String, Object> getRedbagStatus() {
        Integer btUserId = SecurityUtils.getBtUserId();
        Map<String, Object> map = new HashMap<>();
        String rpCode = "";//抽奖编号
        String hhdd = "";//页面显示
        int type = 1;//类型 1日 2周
        int rpCodeType = 1;//抽奖编号 类型 1今天 2明天

        BtRedbagConfig dayConfig = configMapper.selectBtRedbagConfigById(1); //日配置
        BtRedbagConfig weekConfig = configMapper.selectBtRedbagConfigById(2); //周配置
        Integer dayOpenHour = dayConfig.getOpenHour();//日开奖时间
        Integer weekOpenHour = weekConfig.getOpenHour();//周开奖时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        Date today = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(today);
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int week = c.get(Calendar.DAY_OF_WEEK);// 1周日 7周六
        BtRedbagPoll poll = new BtRedbagPoll();
        if((week == 1 && hour < weekOpenHour)){ //周日并且再开奖之前
            type = 2;
            rpCodeType = 1;
        }else if (week == 7 && hour >= dayOpenHour){ //周六开奖之后
            type = 2;
            rpCodeType = 2;
        }else{
            if(hour < dayOpenHour){
                rpCodeType = 1;
            }else{
                rpCodeType = 2;
            }
        }
        if(rpCodeType == 1){
            hhdd = (calendar.get(Calendar.MONTH) + 1) + "月" + calendar.get(Calendar.DAY_OF_MONTH)+"日";
            rpCode = DateUtils.dateTimehh(null);
        }else{
            calendar.add(Calendar.DAY_OF_MONTH, +1);//明天
            hhdd = (calendar.get(Calendar.MONTH) + 1) + "月" + calendar.get(Calendar.DAY_OF_MONTH)+"日";
            rpCode = DateUtils.dateTimehh(calendar.getTime());
        }
        //查询当前开放的池
        map = btRedbagPollMapper.selectBtRedbagPollByRpCode(rpCode,null);
        if(StringUtils.isNull(map) || map.size() == 0){ //没有则新建
            map = new HashMap<>();
            poll.setType(type);
            poll.setRpCode(rpCode);
            poll.setCreateTime(DateUtils.getNowDate());
            btRedbagPollMapper.insertBtRedbagPoll(poll);
            map.put("status",10);//可以加入
        }else{
            //查询当前开放的池  用户是否参加
            map = btRedbagPollMapper.selectBtRedbagPollByRpCode(rpCode,btUserId);
            if(StringUtils.isNull(map) || map.size() == 0){
                map = new HashMap<>();
                map.put("status",10);//可以加入
            }
        }
        map.put("openTime",hhdd +" "+ (type == 1 ? dayOpenHour : weekOpenHour)+ ":00");
        map.put("type",type);
        return map;
    }

    /**
     * 修改红包玩法池
     * 
     * @param btRedbagPoll 红包玩法池
     * @return 结果
     */
    @Override
    public int updateBtRedbagPoll(BtRedbagPoll btRedbagPoll) throws Exception
    {
        Integer rpId = btRedbagPoll.getRpId();
        BtRedbagPoll poll = btRedbagPollMapper.selectBtRedbagPollById(rpId);
        Integer status = poll.getStatus();
        if(status != 0){
            throw new CustomException("未开奖状态才可以修改！");
        }
        btRedbagPoll.setUpdateTime(DateUtils.getNowDate());
        btRedbagPoll.setUpdateBy(SecurityUtils.getUsername());
        return btRedbagPollMapper.updateBtRedbagPoll(btRedbagPoll);
    }

    /**
     * 批量删除红包玩法池
     * 
     * @param rpIds 需要删除的红包玩法池ID
     * @return 结果
     */
    @Override
    public int deleteBtRedbagPollByIds(Integer[] rpIds)
    {
        return btRedbagPollMapper.deleteBtRedbagPollByIds(rpIds);
    }

    /**
     * 删除红包玩法池信息
     * 
     * @param rpId 红包玩法池ID
     * @return 结果
     */
    @Override
    public int deleteBtRedbagPollById(Integer rpId)
    {
        return btRedbagPollMapper.deleteBtRedbagPollById(rpId);
    }

    /**
     * 提前开奖
     *
     * @param rpId 红包玩法池ID
     * @return 结果
     */
    @Override
    public int updateBtRedPoolPublishTime(Integer rpId) {
        return btRedbagPollMapper.updateBtRedPoolPublishTime(rpId);
    }

    public static void main(String[] args) {
        Date today = new Date();
        Calendar cal  = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DATE,-1);
        cal.set(Calendar.HOUR_OF_DAY, 19);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        System.out.println(cal.getTime());
    }
}
